package com.suitsoft.ut.web.open;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.annotations.Token;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 写开题报告
 *@author Suineg
 */
public class ReportWriteAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5193068359371988001L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private StudentManager studentManager;
	
	private ThesisFileManager fileManager;
	
	private Student student;
	
	@Override
	public String execute() throws Exception {
		student = studentManager.findByUser(UserHolder.getLoginUser().getId());
		if(student == null){
			addActionMessage("请用学生账户登陆！");
			return "suc";
		}
		thesis = student.getThesis();
		if(thesis == null){
			addActionMessage("你还没有已确定的题目！！！！");
			return "suc";
		}
		thesisFile = thesis.getThesisFile();
		if(thesisFile.getThesisStatus() > Constants.THSIS_STAUTS_KAITI_GROPU_AUDIT_REPORT){
			addActionMessage("开题报告已经到审核环节。不能修改");
			return "suc";
		}
		return SUCCESS;
	}
	
	@Token
	@LogAnnotation(log ="填写开题报告")
	public String save(){
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return "suc";
		}
		try {
			thesis = thesisManager.get(id);
			ThesisFile ft = thesisFile;
			thesisFile = thesis.getThesisFile();
			
			
			thesisFile.setOpenReportContent(ft.getOpenReportContent());
			thesisFile.setOpenReportScheldule(ft.getOpenReportScheldule());
			thesisFile.setOpenReportReference(ft.getOpenReportReference());
			thesisFile.setOpenReportManual(ft.getOpenReportManual());
			
			thesisFile.setOpenReportStudent(studentManager.findByUser(UserHolder.getLoginUser().getId()).getStudentName());
			thesisFile.setOpenReportStudentDate(new Date());
			
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_KAITI_WRITE_REPORT);
			fileManager.save(thesisFile);
			addActionMessage("操作成功！");
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
			return "suc";
		}
		return "suc";
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
	
}

